﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebCrawler.HtmlParser.Interfaces;

namespace WebCrawler.HtmlParser
{
    public class RegexParser : IHtmlParser
    {
        private string _serverUrl;
        private string _pathUrl;
        private string _content;

        public RegexParser(string serverUrl, string pathUrl, string content)
        {
            _serverUrl = serverUrl;
            _pathUrl = pathUrl;
            _content = content;
        }

        public static string GetDefaultPage(IHtmlDoc doc)
        {
            
            if (doc.Url.LastIndexOf('/') != doc.Url.Length - 1)
                doc.Url += "/";

            if (!doc.Url.Contains("/index") || doc.Url.Contains("/default"))
            {
                doc.Url += "index.htm";
            }
            string url = doc.Url;
            ;
            if (!url.StartsWith("http://"))
            {
                url = "http://" + url;
            }
            doc.URI = new Uri(url);
            return doc.Url;
        }

        public IList<string> GetHrefs()
        {
            List<string> hrefs = new List<string>();
            
            if (_content == null)
                return hrefs;

            Regex r = new Regex("href.*?\"(?<href>.*?)\"",
                RegexOptions.IgnoreCase | RegexOptions.Singleline);

            MatchCollection mc = r.Matches(_content);

            

            foreach (Match m in mc)
            {
                string href = m.Groups["href"].Value;
                href = href.Trim();

                if (String.Compare(FromLeft(href, 10), "javascript", true) == 0)
                {
                    continue;
                    //if (!href.Contains("http"))
                    //    continue;

                    //int i = href.IndexOf("http");
                    //int l = href.LastIndexOf("/");
                    //href = href.ToLower().Substring(i, l - i);
                }

                if (!String.IsNullOrWhiteSpace(href))
                {
                    // if url doesn't start with ... 
                    if (FromLeft(href, 1) != "#")
                    {
                        // ... but with.
                        if (FromLeft(href, 1) == "/")
                        {
                            href = _serverUrl + href;
                        }
                        else
                        {
                            // else if no http beginning...
                            if (String.Compare(FromLeft(href, 7), "http://", true) != 0)
                            {
                                // ... but begin with www.
                                if (String.Compare(FromLeft(href, 3), "www", true) == 0)
                                {
                                    href = "http://" + href;
                                }
                                else
                                {
                                    href = _pathUrl + href;
                                } // end else
                            } // end if 
                        } // end else
                    } // end if

                    hrefs.Add(href.ToLower());

                } // end if
            } // end foreach

            return hrefs;
        }

        /// <summary>
        /// Check if href is longer than string to compare.
        /// If true, return string of same size as comparing string.
        /// </summary>
        private string FromLeft(string href, int comparingLength)
        {
            if (href.Length > comparingLength)
            {
                href = href.Substring(0, comparingLength);
            } // end if

            return href;
        }

        public string GetPageTitle()
        {
            Regex regex = new Regex("<title>(.*)</title>");
            string match = regex.Match(_content).Groups[0].Value;
            return match;
        }
    }
}
